import json
from .. import db
from ..models import Userinfo

def save_userinfo(result):
    json_result = json.loads(result)

    data = json_result.get('result')[0]

    userinfo = Userinfo(uid=data.get('uid'),
                        name=data.get('name'),
                        grade=data.get('grade'),
                        college=data.get('college'),
                        specialty=data.get('specialty'))

    db.session.add(userinfo)
    db.session.commit()

def query_userinfo(uid):
    userinfo = Userinfo.query.filter_by(uid=uid).first()
    if userinfo == None:
        return None
    else:
        return userinfo
